// mock数据模拟
import Mock from 'mockjs'
import http from 'axios';
import Cookies from "js-cookie";


// get请求从config.url获取参数，post从config.body中获取参数
function param2Obj (url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse(
    '{"' +
    decodeURIComponent(search)
      .replace(/"/g, '\\"')
      .replace(/&/g, '","')
      .replace(/=/g, '":"') +
    '"}'
  )
}
let List = []
let username = ""
let hostname = ""
let counter = 0
// let flag = false
function getLog(){
  console.log(Cookies.get('token'))

}





export default {

    // 获取检测结果
    getStatisticalData: config => {
      let pageList = []
      let alarmCount = {'emergency':0, 'alert':0, 'critical':0, 'error':0, 'warning':0, 'notice':0, 'informational':0, 'debug':0}
      let mockList = []

      http.post('http://43.138.136.88:3007/my/getLog',null,{
        headers: {
          'Authorization' : Cookies.get('token')
        }
      })
      .then((res) => {
        List = res.data.message
        console.log("getList")
        if(List.length > 0){
          hostname = List[0]["hostname"]
        }
    
        // 给每一条数据加个id
        for(let i=0;i<=List.length-1;++i){
          List[i]["id"] = Mock.Random.guid()
        }

      })
      .then( (res) => {
        console.log("operate List")
        // 分页操作
        const { name, page = 1,limit = 20,  } = param2Obj(config.url)
  
        console.log('name:' + name, 'page:' + page, '分页大小limit:' + limit)
        mockList = List.filter(log => {
          if (name && log.time.indexOf(name) === -1) return false
          return true
        })
  
        pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
  
        for(let i=0;i<=List.length-1;++i){
          alarmCount[List[i]["priority"]]++
        }

        return {
          code: 200,
          data: {
            username: username,
            count: mockList.length,
  
            // 未处理告警数信息饼图
            alarmData: [
              {
                name: 'emergency',
                value: alarmCount['emergency']
              },
              {
                name: 'alert',
                value: alarmCount['alert']
              },
              {
                name: 'critical',
                value: alarmCount['critical']
              },
              {
                name: 'error',
                value: alarmCount['error']
              },
              {
                name: 'warning',
                value: alarmCount['warning']
              },
              {
                name: 'notice',
                value: alarmCount['notice']
              },
              {
                name: 'informational',
                value: alarmCount['informational']
              },
              {
                name: 'debug',
                value: alarmCount['debug']
              },
              
            ],
  
            // 分页告警信息
            tableData: pageList,
  
            // 用户信息
            userInfor: {
              '主机名称': hostname,
              '主机ID码': "i-123456",
              '操作系统': "linux",
              '公网IPv4': "-",
              '公网IPv6': "-",
              '私网IPv4': "10.2.0.36",
              '私网IPv6': "-",
              '地域信息': "default",
              '内核版本': "3.10.0",
            },
  
            // 启用信息
            onOffInfor: {
              '上次结束时间': "2022-3-18 12:00:35",
              '启动持续时间': "0小时0分钟"
            }
  
          }
        }
      })


    },

    /**
   * 删除
   * @param id
   * @return {*}
   */
     deleteLog: config => {
      const { id } = param2Obj(config.url)
      console.log(id)

      if (!id) {
        return {
          code: -999,
          message: '参数不正确'
        }
      } 
      else {
             http.post('http://43.138.136.88:3007/my/deleteLog',
                'time='+id,
                {
                    headers:{
                      'Authorization' : Cookies.get('token')
                    }
                }
             ).then((res) => {
                // console.log(res.data.st)
                if(res.data.status == 0){
                  // getLog()
                  return {
                    code: 20000,
                    message: '删除成功'
                  }
                }
                else{
                  return {
                    code: 0,
                    message: '删除失败!'
                  }
                }
             })
            
      }


      }
    }
